home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / DELPHI.SWG / 0030_Loading Bitmaps and Cursors from RES Fil.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  4.2 KB  |  125 lines

  1.  
  2.          Loading Bitmaps and Cursors from RES files
  3.  
  4. Bitmaps and cursors can be stored in a resource (RES) files and
  5. linked into your application's EXE file.  RES files can be created
  6. with Delphi's Image Editor or Borland's Resource Workshop that comes
  7. with the Delphi RAD Pack.  Bitmaps and cursors stored in RES files
  8. (after being bound into an EXE or DLL) can be retrieved by using the
  9. API functions LoadBitmap and LoadCursor, respectively.
  10.  
  11.  
  12. Loading Bitmaps
  13. ---------------
  14. The LoadBitmap API call is defined as follows:
  15.  
  16. function LoadBitmap(Instance: THandle;
  17.                     BitmapName: PChar): HBitmap;
  18.  
  19. The first parameter is the instance handle of the module (EXE or DLL)
  20. that contains the RES file you wish to get a resource from.  Delphi
  21. provides the instance handle of the EXE running in the global variable
  22. called Hinstance.  For this example it is assumed that the module that
  23. you are trying to load the bitmap from is your application.  However,
  24. the module could be another EXE or DLL file.  The following example
  25. loads a bitmap called BITMAP_1 from a RES file linked into the
  26. application's EXE:
  27.  
  28. procedure TForm1.Button1Click(Sender: TObject);
  29. var
  30.   Bmp: TBitmap;
  31. begin
  32.   Bmp := TBitmap.Create;
  33.   Bmp.Handle := LoadBitmap(HInstance,'BITMAP_1');
  34.   Canvas.Draw(0, 0, Bmp);
  35.   Bmp.Free;
  36. end;
  37.  
  38. There is one drawback to using the LoadBitmap API call though LoadBitmap
  39. is a Windows 3.0 API call and loads in bitmaps only as DDBs (Device
  40. Dependent Bitmaps).  This can cause color palette problems when retrieving
  41. DIBs (Device Independent Bitmaps) from RES files.  The code listed below
  42. can be used to retrieve DIBs from RES files.  This code loads the bitmap
  43. as a generic resource, puts it into a stream, and then does a
  44. LoadFromStream call which causes Delphi to realize the color palette
  45. automatically.
  46.  
  47. procedure TForm1.Button1Click(Sender: TObject);
  48. const
  49.   BM = $4D42;  {Bitmap type identifier}
  50. var
  51.   Bmp: TBitmap;
  52.   BMF: TBitmapFileHeader;
  53.   HResInfo: THandle;
  54.   MemHandle: THandle;
  55.   Stream: TMemoryStream;
  56.   ResPtr: PByte;
  57.   ResSize: Longint;
  58. begin
  59.   BMF.bfType := BM; 
  60.   {Find, Load, and Lock the Resource containing BITMAP_1}
  61.   HResInfo := FindResource(HInstance, 'BITMAP_1', RT_Bitmap);
  62.   MemHandle := LoadResource(HInstance, HResInfo);
  63.   ResPtr := LockResource(MemHandle);
  64.  
  65.   {Create a Memory stream, set its size, write out the bitmap
  66.    header, and finally write out the Bitmap                  }
  67.   Stream := TMemoryStream.Create;
  68.   ResSize := SizeofResource(HInstance, HResInfo);
  69.   Stream.SetSize(ResSize + SizeOf(BMF));
  70.   Stream.Write(BMF, SizeOf(BMF));
  71.   Stream.Write(ResPtr^, ResSize);
  72.  
  73.   {Free the resource and reset the stream to offset 0}
  74.   FreeResource(MemHandle);
  75.   Stream.Seek(0, 0);
  76.   {Create the TBitmap and load the image from the MemoryStream}
  77.   Bmp := TBitmap.Create;
  78.   Bmp.LoadFromStream(Stream);
  79.   Canvas.Draw(0, 0, Bmp);
  80.   Bmp.Free;
  81.   Stream.Free;
  82. end;
  83.  
  84.  
  85. Loading Cursors
  86. -------------
  87. The LoadCursor API call is defined as follows:
  88.  
  89. function LoadCursor(Instance: THandle;
  90.                     CursorName: PChar): HCursor;
  91.  
  92. The first parameter is the Instance variable of the module that
  93. contains the RES file.  As above, this example assumes that the
  94. module that you are trying to load the cursor from is your
  95. application.  The second parameter is the name of the cursor.
  96.  
  97. Under the interface section declare:
  98.  
  99. const
  100.   crMyCursor = 5; {Other units can use this constant}
  101.  
  102. Next, add the following two lines of code to the form's OnCreate
  103. event as follows:
  104.  
  105. procedure TForm1.FormCreate(Sender: TObject);
  106. begin
  107.   Screen.Cursors[crMyCursor] := LoadCursor(HInstance, 'CURSOR_1');
  108.   Cursor := crMyCursor;
  109. end;
  110.  
  111. or you may want to change one of the standard Delphi cursors as
  112. follows (the Cursor constants can be found in the On-line Help
  113. under Cursors Property):
  114.  
  115.  
  116. procedure TForm1.FormCreate(Sender: TObject);
  117. begin
  118.   {This example changes the SQL Hourglass cursor}
  119.   Screen.Cursors[crSQLWait] := LoadCursor(HInstance, 'CURSOR_1');
  120. end;
  121.  
  122. Note:  Normally it is necessary to delete any cursor resources with the
  123. DeleteCursor, however, in Delphi this is not necessary because Delphi
  124. will delete the all cursors in the Cursors array.
  125.